package com.example.myfirstapp;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Fragment;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;
import db.Horarios;

public class CargarHorariosActivity extends Activity {
	
	EditText etGanancia;
	TextView tvFechaInicio,tvFechaFin;
	
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_cargar_horarios);

		if (savedInstanceState == null) {
			getFragmentManager().beginTransaction()
					.add(R.id.container, new PlaceholderFragment()).commit();
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.cargar_horarios, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	/**
	 * A placeholder fragment containing a simple view.
	 */
	public static class PlaceholderFragment extends Fragment {

		public PlaceholderFragment() {
		}

		@Override
		public View onCreateView(LayoutInflater inflater, ViewGroup container,
				Bundle savedInstanceState) {
			View rootView = inflater.inflate(R.layout.fragment_cargar_horarios,
					container, false);
			return rootView;
		}
	}
	
	
	@Override protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		
		LinearLayout inicio = (LinearLayout) findViewById(R.id.inicio);
		inicio.setOnClickListener(new OnClickListener() {

	        @Override
	        public void onClick(View v) {
	        	fechaPopUpInicio();
//	            Toast.makeText(getApplicationContext(), "Hello World!",
//	                    Toast.LENGTH_LONG).show();

	        }
	    });
		
		LinearLayout fin = (LinearLayout) findViewById(R.id.fin);
		fin.setOnClickListener(new OnClickListener() {

	        @Override
	        public void onClick(View v) {
	        	fechaPopUpFin();
//	            Toast.makeText(getApplicationContext(), "chau World!",
//	                    Toast.LENGTH_LONG).show();

	        }
	    });
		
		
	}
	
	
	protected void fechaPopUpInicio(){
		final Calendar c = Calendar.getInstance();
		int mYear = c.get(Calendar.YEAR);
		int mMonth = c.get(Calendar.MONTH);
		int mDay = c.get(Calendar.DAY_OF_MONTH);
		 	
		DatePickerDialog dpd = new DatePickerDialog(this,
		        new DatePickerDialog.OnDateSetListener() {
		 
		            @Override
		            public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
		            	 if (view.isShown()) {
		            	TextView tvFecha = (TextView) findViewById(R.id.tvFechaInicio);
		            	tvFecha.setText(((dayOfMonth < 10) ? "0" + dayOfMonth : dayOfMonth ) + "/" + (((monthOfYear + 1) < 10) ? "0" + (monthOfYear + 1) : (monthOfYear + 1) )  + "/" + year);
		            	horaPopUpInicio();
		            	 }
		            }
		        }, mYear, mMonth, mDay);
		dpd.show();
	}
	
	protected void horaPopUpInicio(){
		final Calendar c = Calendar.getInstance();
		int mHour = c.get(Calendar.HOUR_OF_DAY);
		int mMinute = c.get(Calendar.MINUTE);
		
		TimePickerDialog tpd = new TimePickerDialog(this,
		        new TimePickerDialog.OnTimeSetListener() {
		 
		            @Override
		            public void onTimeSet(TimePicker view, int hourOfDay,
		                    int minute) {
		            	if (view.isShown()) {
		            	TextView tvFecha = (TextView) findViewById(R.id.tvFechaInicio);
		            	tvFecha.setText(tvFecha.getText() + " " + ((hourOfDay < 10) ? "0" + hourOfDay : hourOfDay )  + ":" + ((minute < 10) ? "0" + minute : minute ) );
		            	}
		            }
		        }, mHour, mMinute, true);
		tpd.show();
	}
	
	protected void fechaPopUpFin(){
		final Calendar c = Calendar.getInstance();
		int mYear = c.get(Calendar.YEAR);
		int mMonth = c.get(Calendar.MONTH);
		int mDay = c.get(Calendar.DAY_OF_MONTH);
		 	
		DatePickerDialog dpd = new DatePickerDialog(this,
		        new DatePickerDialog.OnDateSetListener() {
		 
		            @Override
		            public void onDateSet(DatePicker view, int year,int monthOfYear, int dayOfMonth) {
		            	 if (view.isShown()) {
		            	TextView tvFecha = (TextView) findViewById(R.id.tvFechaFin);
		            	tvFecha.setText(dayOfMonth + "/" + (monthOfYear + 1) + "/" + year);
		            	horaPopUpFin();
		            	 }
		            }
		        }, mYear, mMonth, mDay);
		dpd.show();
	}
	
	protected void horaPopUpFin(){
		final Calendar c = Calendar.getInstance();
		int mHour = c.get(Calendar.HOUR_OF_DAY);
		int mMinute = c.get(Calendar.MINUTE);
		
		TimePickerDialog tpd = new TimePickerDialog(this,
		        new TimePickerDialog.OnTimeSetListener() {
		 
		            @Override
		            public void onTimeSet(TimePicker view, int hourOfDay,
		                    int minute) {
		            	if (view.isShown()) {
		            	TextView tvFecha = (TextView) findViewById(R.id.tvFechaFin);
		            	tvFecha.setText(tvFecha.getText() + " " + hourOfDay + ":" + minute);
		            	}
		            }
		        }, mHour, mMinute, true);
		tpd.show();
	}
	
	
	
	public void btnGuardar(View view) {
		etGanancia = (EditText) findViewById(R.id.etGanancia);
		tvFechaInicio = (TextView) findViewById(R.id.tvFechaInicio);
		tvFechaFin = (TextView) findViewById(R.id.tvFechaFin);
		
		String fecha,fechaInicio,fechaFin;
		fecha = tvFechaInicio.getText().toString().substring(0,10);
		fechaInicio = tvFechaInicio.getText().toString();
		fechaFin =  tvFechaFin.getText().toString();
		
		int ganancia = Integer.valueOf(etGanancia.getText().toString());
		
		Horarios h = new Horarios();
		h.guardar(getApplicationContext(), fecha, fechaInicio, fechaFin, ganancia);
		
//		SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm");
//		SimpleDateFormat format2 = new SimpleDateFormat("dd/MM/yyyy");
		
		
//		try {
//			fecha = format2.parse(tvFechaInicio.getText().toString().substring(0,10));
//			fechaInicio = format.parse(tvFechaInicio.getText().toString());
//			fechaFin = format.parse(tvFechaFin.getText().toString());
			
			
			
//		} catch (ParseException e) {
//			e.printStackTrace();
//			Toast.makeText(getApplicationContext(), "Error en parseo!", Toast.LENGTH_LONG).show();
//		}
		
	
		
//		dpFecha = (DatePicker) findViewById(R.id.spFecha);
//		tpFechaInicio =  (TimePicker) findViewById(R.id.tpHoraInicio);
//		tpFechaFin =  (TimePicker) findViewById(R.id.tpHoraFin);
	
//		
//		int ganancia = Integer.valueOf(etGanancia.getText().toString());
//		int day = dpFecha.getDayOfMonth();
//		 int month = dpFecha.getMonth() + 1;
//		 int year = dpFecha.getYear();
//		 int hour = tpFechaInicio.getCurrentHour();
//		 int minute = tpFechaInicio.getCurrentMinute();
	}
	

}
